package com.cu;
import java.sql.*;
public class test {
    public static void main(String[] args) throws Exception {
        prepare();
    }
    public static void prepare() throws Exception {
        String driver = "com.mysql.jdbc.Driver";    //驱动
        String url = "jdbc:mysql://localhost:3306/test1";   //链接
        String sql = "select ROLEID,ROLENAME from ORGROLE where ROLEID in ((SELECT ROLEID from ORGUSER WHERE USERID ='admin'),(SELECT ROLEID from ORGMAP WHERE USERID ='admin'))";
        Class.forName(driver);
        Connection cn = DriverManager.getConnection(url,  //获取链接
                "root","root");
        PreparedStatement ps = cn.prepareStatement(sql);  //返回预编译对象
        //查询
        ResultSet rs = ps.executeQuery();
        //打印列标题
        int cols = rs.getMetaData().getColumnCount();   //列的总数
        for(int i=1; i<=cols; i++){
            //打印列的名称
            System.out.print( rs.getMetaData().getColumnName(i)+"\t");
        }
        //打印数据
        while(rs.next()){   //每次向下取一条，直到结尾
            System.out.println();   //换行
            for(int i=1; i<=cols; i++){
                System.out.print( rs.getString(i)+"\t");
            }
        }
    }
}
